
var indexId = 0;
var listIdArrayRaw = new Array();
var container = '/pdfjs/web/pdf_main_container.html';
var curfileName;


/**
 * 初始化
 * 
 * @param res
 */
function initSuccess() {
	var files = getFlileMd5PathList();
	if(files==null || files.length==0){
		alert('没有课预览的文件');
		return;
	}
	viewIframe(files);
}



/**
 * 初始化iframe
 */
	var	viewIframe = function(filedatas) {
			
				for (var i = 0; i < filedatas.length; i++) {
					var item = filedatas[i];
					item.id=item.fileId.replace('@', '_');
					$("#attachmentsView").append( '<li class="viewerListfont" id="' + item.id + '"><a>' + item.fileName + '</a></li>');
					listIdArrayRaw.push(item.fileId.replace('@', '_'));
			
					$('#' + item.id).on("click", function() {
						refreshFrame($(this).attr('id'));
					})
				}
				initFirstShow();
	}
	var initFirstShow = function(){
		var index = getInitShowFileIndex();
		var initId = listIdArrayRaw[index].replace(/_/, '@');
		$('#' + listIdArrayRaw[index]).addClass('hse2');
		curfileName=getFileName($('#' + listIdArrayRaw[index]).text());
		$("#myframe").attr("src", container + "?md5Path=" + initId);
	}


	var refreshFrame = function(id) {
		$('#' + id).addClass('hse2');
		curfileName=getFileName($('#' + id).text());
		
		indexId = listIdArrayRaw.indexOf(id);
		var md5Path = id.replace(/_/, '@');
		$("#myframe").attr("src", container + "?md5Path=" + md5Path)
	}
	var childResfreFrame=function() {
		var size = listIdArrayRaw.length;
		if (size == indexId + 1) {
			return;
		} else {
			refreshFrame(listIdArrayRaw[indexId + 1]);
		}
	
	}
	var getFileName = function(fileName){
		if(fileName.length>26){
			return fileName.substring(0,24)+"...";
		}else{
			return fileName;
		}
	}
	var childResToggle = function() {
		$("#list").toggle();
		
	}
	$(function (){
		
		initSuccess();
	});
